package cn.edu.hpu.jie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cn.edu.hpu.jie.entity.User;
import cn.edu.hpu.jie.entity.UserInfo;
import cn.edu.hpu.jie.util.RowMapping;
import cn.edu.hpu.jie.util.VoteDB;

/**
 * 对t_user和t_userinfo表的操作
 * @author Jie
 *
 */
public class UserDao {
	private VoteDB<User> vdb = new VoteDB<User>();
	private VoteDB<UserInfo> vdbi = new VoteDB<UserInfo>();
	
	private class UserMapping implements RowMapping<User>{
		public User getMapping(ResultSet rs) throws SQLException {
			User user = new User();
			user.setId(rs.getInt("id"));
			user.setUsername(rs.getString("username"));
			user.setPassword(rs.getString("password"));
			user.setRole(rs.getInt("role"));
			user.setEnable(rs.getBoolean("enable"));
			return user;
		}
	}
	
	private class UserInfoMapping implements RowMapping<UserInfo>{
		public UserInfo getMapping(ResultSet rs) throws SQLException {
			UserInfo ui = new UserInfo();
			ui.setId(rs.getInt("id"));
			ui.setUsername(rs.getString("username"));
			ui.setRole(rs.getInt("role"));
			ui.setEnable(rs.getBoolean("enable"));
			ui.setName(rs.getString("name"));
			ui.setEmail(rs.getString("email"));
			ui.setOfficephone(rs.getString("office_phone"));
			ui.setMobilphone(rs.getString("mobilphone"));
			ui.setDepartment(rs.getString("department"));
			return ui;
		}
	}
	
	/**
	 * 根据username获取user对象
	 * 主要用于登入
	 * @param userName
	 * @return 符合条件的记录
	 */
	public User getUserByUserName(String userName) {
		String sql = "SELECT id, username, password, role, enable FROM t_user WHERE username = ?";
		return vdb.getObject(sql, new UserMapping(), userName);
	}
	
	/**
	 * 根据id获取user的详细信息
	 * 主要用于查看个人信息
	 * @param id
	 * @return 符合条件的记录
	 */
	public UserInfo getUserInfoById(int id) {
		String sql = "SELECT t_user.id AS id, username, role, enable, name, email, office_phone, mobilphone, department FROM t_user, t_userinfo WHERE t_user.id = ? AND t_user.id = t_userinfo.id";
		return vdbi.getObject(sql, new UserInfoMapping(), id);
	}
	
	/**
	 * 超级管理员设置系统用户的可用性
	 * @param id
	 * @param enable
	 * @return 受影响的行数
	 */
	public int updateUserBySuperAdmin(int id, boolean enable) {
		String sql = "UPDATE t_user SET enable = ? WHERE id = ?";
		return vdb.updateData(sql, enable, id);
	}
	
	/**
	 * 用户自己修改密码
	 * @param id
	 * @param password
	 * @return
	 */
	public int modifyPasswordByOwer(int id, String password) {
		String sql = "UPDATE t_user SET password = ? WHERE id = ?";
		return vdb.updateData(sql, password, id);
	}
	
	/**
	 * 系统用户修改个人信息
	 * @param ui
	 * @return
	 */
	public int modifyOwerInfo(UserInfo ui) {
		String sql = "UPDATE t_userinfo SET name = ?, email = ?, office_phone = ?, mobilphone = ?, department = ? WHERE id = ?";
		return vdbi.updateData(sql, ui.getName(), ui.getEmail(), ui.getOfficephone(), ui.getMobilphone(), ui.getDepartment(), ui.getId());
	}
	
	/**
	 * 新增一个系统用户
	 * 超级管理员使用
	 * @param ui
	 * @return
	 */
	public int insertUser(User u) {
		String sql = "INSERT INTO t_user (username, password, role, enable) VALUES(?, ?, ?, ?)";
		return vdb.getInsertPrimaryKey(sql, u.getUsername(), u.getPassword(), u.getRole(), u.isEnable());
	}
	
	/**
	 * 新用户输入自己的信息
	 * 第一次登入系统后必须填写
	 * @param ui
	 * @return
	 */
	public int insertUserInfo(UserInfo ui) {
		String sql = "INSERT INTO t_userinfo (id, name, email, office_phone, mobilphone, department) VALUES(?, ?, ?, ?, ?, ?)";
		return vdb.updateData(sql, ui.getId(), ui.getName(), ui.getEmail(), ui.getOfficephone(), ui.getMobilphone(), ui.getDepartment());
	}
	
	/**
	 * 获取所有的用户的信息
	 * 用于超级管理员
	 * @return 所有的user记录
	 */
	public List<UserInfo> getAllUser() {
		String sql = "SELECT t_user.id AS id, username, role, enable, name, email, office_phone, mobilphone, department FROM t_user, t_userinfo WHERE t_user.id = t_userinfo.id";
		return vdbi.getAll(sql, new UserInfoMapping());
	}
}
